/**
 * 
 */
package com.ting.template.core.common.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>注释</p>
 * @author Your name
 * @version $Id: DateUtils.java, v 0.1 2017年6月16日 下午5:25:21 雷霆 Exp $
 */
public class DateUtils {
	public static final  String YYYY_MM_DD="yyyy-MM-dd";
	public static final  String YYYYMM="yyyyMM";
	public static final  String YYYY_MM_DD_HH_MM_SS="yyyy-MM-dd HH:mm:ss";

	public static SimpleDateFormat getSdf(String format) {
		return new SimpleDateFormat(format);
	}



	public static Date parserDate(String dateStr,String format) {
		if(StringUtils.isEmpty(dateStr)||StringUtils.isEmpty(format)){
			return null;
		}

		Date d;
		try {
			d = getSdf(format).parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			d = null;
		}
		return d;
	}

	public static Date parseFromYYYYMM(String dateStr){
		return parserDate(dateStr,YYYYMM);
	}
	public static Date parseFromYYYY_MM_DD(String dateStr){
		return parserDate(dateStr,YYYY_MM_DD);
	}
	public static Date parseFromYYYY_MM_DD_HH_MM_SS(String dateStr){
		return parserDate(dateStr,YYYY_MM_DD_HH_MM_SS);
	}




	/**
	 * 传入一个日期，返回下月的开始时间
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getStartOfNextMonth(Date date){
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, +1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}


	/**
	 * 传入一个日期，返回上月的开始时间
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getStartOfPreMonth(Date date){
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 传入一个日期，返回当月的开始时间
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getStartOfMonth(Date date){
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 传入一个日期，返回上月的结束时间
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getEndOfPreMonth(Date date)throws Exception{
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
	

	public static String getYearAndMonth(Date date) {
		if(null==date){
			date =new Date();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		if(month<10){
			return year+"0"+month;
		}
		return year+""+month;
	}

	public static void main(String[] args) {
		try {

			String dateStr="201712";
			Date date=parseFromYYYYMM(dateStr);
			//String date=getYearAndMonth(getStartOfPreMonth(getStartOfPreMonth(getStartOfPreMonth(new Date()))));
			System.out.print(date);
		}catch (Exception w){

		}
	}


}
